﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using Microsoft.Practices.Unity;

using Happy.ExtentionMethods;
using Happy.Bootstrap;

namespace Happy.Bootstrap.Unity
{
    /// <summary>
    /// 扩展<see cref="IBootstrapService"/>。
    /// </summary>
    public static class IBootstrapServiceExtentions
    {
        private static IUnityContainer _CurrentUnityContainer;

        /// <summary>
        /// 和Unity进行集成。
        /// </summary>
        public static IBootstrapService IntegrateWithUnity(this IBootstrapService that)
        {
            that.MustNotNull("that");

            return that.IntegrateWithUnity(new UnityContainer());
        }

        /// <summary>
        /// 和Unity进行集成。
        /// </summary>
        public static IBootstrapService IntegrateWithUnity(this IBootstrapService that, IUnityContainer unityContainer)
        {
            that.MustNotNull("that");
            unityContainer.MustNotNull("unityContainer");

            _CurrentUnityContainer = unityContainer;

            return that.UseServiceLocator(new UnityServiceLocator(unityContainer));
        }

        /// <summary>
        /// 使用按照约定注册插件。
        /// </summary>
        public static RegisterServiceByConventionPlugin UseRegisterServiceByConventionPlug(this IBootstrapService that)
        {
            that.MustNotNull("that");

            var plug = new RegisterServiceByConventionPlugin(_CurrentUnityContainer);

            that.AddPlugin(plug);

            return plug;
        }
    }
}
